﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace BOMS.Lib
{
    public class Log   
    {   
        private string logFile;   
        private StreamWriter writer;   
        private FileStream fileStream = null;
       
        public Log()   
        {   
            //logFile = fileName;  
            logFile=AppDomain.CurrentDomain.BaseDirectory + @"\Log.txt";
            CreateDirectory(logFile);   
        }

        public void log(Exception err)
        {
            string str = err.Message + Environment.NewLine + err.StackTrace;
            log(str);
        }
        public void log(string info)   
        {   
  
            try  
            {   
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile);   
                if (!fileInfo.Exists)   
                {   
                    fileStream = fileInfo.Create();   
                    writer = new StreamWriter(fileStream);   
                }   
                else  
                {   
                    fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write);   
                    writer = new StreamWriter(fileStream);   
                }   
                writer.WriteLine("["+DateTime.Now.ToString("yyyyMMdd HH:mm:ss")+"]: "+Environment.NewLine+ info);   
  
            }   
            finally  
            {   
                if (writer != null)   
                {   
                    writer.Close();   
                    writer.Dispose();   
                    fileStream.Close();   
                    fileStream.Dispose();   
                }   
            }   
        }   
  
        public void CreateDirectory(string infoPath)   
        {   
            DirectoryInfo directoryInfo = Directory.GetParent(infoPath);   
            if (!directoryInfo.Exists)   
            {   
                directoryInfo.Create();   
            }   
        }  
        /*
         Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"\log\Log.txt");
         log.log("内容");

    */ 

    }   
}  

